import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext.webapp import template
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

import random

class Doctor(db.Model):
  name = db.StringProperty(required=True)
  calendar_url = db.StringProperty(required=True)
  photo = db.StringProperty(required=False)

class HomeScreenHandler(webapp.RequestHandler):
  def get(self):
    template_values = {}
    path = os.path.join(os.path.dirname(__file__), 'healthcontrol.html')
    self.response.out.write(template.render(path, template_values))

class ActivitiesHandler(webapp.RequestHandler):
  def get(self):
    template_values = {}
    doctors = Doctor.all()
    names = [doctor.name for doctor in doctors]
    template_values = {'names': names}
    path = os.path.join(os.path.dirname(__file__), 'activities_page.html')
    self.response.out.write(template.render(path, template_values))

class ScheduleHandler(webapp.RequestHandler):
  def get(self):
    doctors = Doctor.all()
    template_values = {}

    names = [doctor.name for doctor in doctors]
    calendar_urls = [doctor.calendar_url for doctor in doctors]
    photos = [doctor.photo for doctor in doctors]

    template_values = {'names': names, 'calendar_urls': calendar_urls, 'photos':photos}

    path = os.path.join(os.path.dirname(__file__), 'doctorapt.html')
    self.response.out.write(template.render(path, template_values))

class NewDoctorHandler(webapp.RequestHandler):
  def get(self):
    self.response.out.write("inside get")
    template_values = {}
    path = os.path.join(os.path.dirname(__file__), 'newdoctor.html')
    self.response.out.write(template.render(path, template_values))

  def post(self):
    self.response.out.write("inside post")
    name = self.request.get('name')
    calendar_url = self.request.get('calendar_url')
    photo = self.request.get('photo')

    if name and calendar_url:
      doctor = Doctor(name=name, calendar_url=calendar_url, photo=photo)
      doctor.put()

    self.get()

def main():
  application = webapp.WSGIApplication([('/', HomeScreenHandler),
                                        ('/activities', ActivitiesHandler),
                                        ('/schedule', ScheduleHandler),
                                        ('/newdoctor', NewDoctorHandler)],
                                       debug=True)
  run_wsgi_app(application)

if __name__ == '__main__':
  main()
